//
//  TableViewCell.m
//  UICollectionViewAutomCell
//
//  Created by lin on 2018/4/23.
//  Copyright © 2018年 qd. All rights reserved.
//

#import "TableViewCell.h"
#import "CollectionViewCell.h"
#import "Model.h"
@interface TableViewCell ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (weak, nonatomic) IBOutlet UICollectionView *collection;

@property (strong, nonatomic) NSArray *dataArr;

@property(nonatomic,strong)NSMutableArray *modelArr;
@end



@implementation TableViewCell

-(NSMutableArray *)modelArr
{
    if (_modelArr == nil) {
        
        _modelArr = [[NSMutableArray alloc] init];
    }
    return _modelArr;
}

- (NSArray *)dataArr{
    if (!_dataArr) {
        _dataArr = @[@"更换刹车片",@"喷漆",@"洗车",@"抛光",@"打蜡",@"室内清洁",@"车载音响"];
    }
    return _dataArr;
}


- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    
//
//    UICollectionViewFlowLayout  *layout = [[UICollectionViewFlowLayout alloc] init];
//    // 设置具体属性
//    // 1.设置 最小行间距
//    layout.minimumLineSpacing = 20;
//    // 2.设置 最小列间距
//        layout. minimumInteritemSpacing  = 10;
//    // 3.设置item块的大小 (可以用于自适应)
//    layout.estimatedItemSize = CGSizeMake(20, 60);
//    self.collection.collectionViewLayout = layout;
//    // 设置滑动的方向 (默认是竖着滑动的)
////    layout.scrollDirection =  UICollectionViewScrollDirectionVertical;
//    // 设置item的内边距
//    layout.sectionInset = UIEdgeInsetsMake(0,10,0,10);
//
    self.collection.showsHorizontalScrollIndicator = NO;
//    self.collection.backgroundColor = [UIColor whiteColor];
    self.collection.delegate = self;
    self.collection.dataSource = self;

    [self.collection registerNib:[UINib nibWithNibName:@"CollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"CollectionViewCell"];
    
    for (NSString *str in self.dataArr) {
        Model *model = [[Model alloc] init];
        model.contentStr = str;
        model.IsSelected = NO;
        [self.modelArr addObject:model];
    }
    
}

#pragma MARK --- UICollectionViewDelegate
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return [self.dataArr count];
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CollectionViewCell" forIndexPath:indexPath];
    cell.model = self.modelArr[indexPath.row];
    return cell;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
   
    Model *model = self.modelArr[indexPath.row];
    model.IsSelected = !model.IsSelected;
    [self.collection reloadData];
    if (model.IsSelected == YES) {
        NSLog(@"选中");
    }else{
        NSLog(@"未选中");
    }
    
}


#pragma mark -布局
// 设置item的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    Model *model = self.modelArr[indexPath.row];
    NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:14]};  //指定字号
    CGRect rect = [model.contentStr boundingRectWithSize:CGSizeMake(0, 30)/*计算宽度时要确定高度*/ options:NSStringDrawingUsesLineFragmentOrigin |
                   NSStringDrawingUsesFontLeading attributes:dic context:nil];

    CGSize size = CGSizeMake(rect.size.width + 10, 40);

    return size;
}

// 设置左右最小间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 10.0;
}

// 设置上下最小间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 10.0;
}

// 设置上左下右间距
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(0, 5, 0, 5);

}



@end
